package management;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class Delete extends  JFrame{

         JButton DeleteButton;
         JLabel DeleteLabel;
         JPanel DeletePanel;
         JTextField DleteText;
         Connection connection;
    JLabel errorLabel;
    ResultSet resultSet;

    //连接数据库
    public void csql() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.err.println("加载错误");
        }
        String username = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/project";
        try {
            connection = DriverManager.getConnection(
                    url,
                    username,
                    password
            );
        } catch (
                SQLException throwables) {
            throwables.printStackTrace();
            System.err.println("数据库在连接期间，出现错误！");
        }
    }
    //删除消费信息窗口
        public void window(){
        csql();
            DeletePanel = new JPanel();
            setContentPane(DeletePanel);
            this.setTitle("删除消费信息");
            setLayout(null);
            DeletePanel.setBackground(Color.LIGHT_GRAY);

            DeleteLabel = new JLabel("消费信息编号");
            DeleteLabel.setBounds(50, 100, 100, 80);
            DeletePanel.add(DeleteLabel);
            Font font = new Font("宋体", Font.BOLD, 15);

            DeleteLabel.setFont(font);
            DleteText = new JTextField();
            DleteText.setBounds(180, 123, 180, 30);
            DeletePanel.add(DleteText);

            errorLabel = new JLabel();
            errorLabel.setBounds(400, 113, 100, 50);  // 根据界面布局需求设置合适的位置
            errorLabel.setForeground(Color.RED);  // 设置文字颜色为红色，使其更醒目，可根据需求调整
            DeletePanel.add(errorLabel);

            DeleteButton = new JButton("删除");
            DeleteButton.setBounds(165, 270, 100, 30);
            DeletePanel.add(DeleteButton);
//            setDefaultCloseOperation(EXIT_ON_CLOSE);
            setBounds(350, 260, 500, 416);
            setVisible(true);
            //删除按钮的监听事件
            DeleteButton.addActionListener(
                    new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            String inputText = DleteText.getText().trim();
                            if (!inputText.isEmpty()) {
                                try {
                                    int num = Integer.parseInt(inputText);
                                    if (checkIdExists(num)) { // 先检查id是否存在
                                        Delete1(num);
                                        JOptionPane.showMessageDialog(null, "消费信息删除成功");
                                        DleteText.setText(""); // 清空输入框
                                        errorLabel.setText(""); // 清空错误提示
                                    } else {
                                        errorLabel.setText("id不存在");

                                    }
                                } catch (NumberFormatException ex) {
                                    JOptionPane.showMessageDialog(null, "请输入有效的整数ID");
                                }
                            } else {
                                JOptionPane.showMessageDialog(null, "请输入消费信息编号");
                            }
                        }
                    }
            );
        }
        //检查数据库中是否存在某个id值
    private boolean checkIdExists(int id) {
        String checkSql = "SELECT COUNT(*) FROM consumer WHERE consumption_id =(?)";
        try (PreparedStatement pstmt = connection.prepareStatement(checkSql)) {
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                int count = rs.getInt(1);
                return count > 0;
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return false;
    }
        //删除id=num的消费信息方法
        public void Delete1(int num){
            String sql = "DELETE  from consumer where consumption_id =(?)";
            try (
                    PreparedStatement pstmt = connection.prepareStatement(sql)) {
                pstmt.setInt(1, num);
                pstmt.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

 public static void main(String[] args) {
      Delete delete = new Delete();
     delete.window();
   }


}
